<template>
     <el-form
     ref="ruleFormRef"
    :model="ruleForm"
    :rules="rules"
    label-width="120px"
    clear
    class="demo-ruleForm"
  
    status-icon
  >
    <el-form-item label="账号" prop="username">
      <el-input v-model="ruleForm.username" />
    </el-form-item>
     <el-form-item label="密码" prop="password">
      <el-input v-model="ruleForm.password" />
    </el-form-item>
     <el-form-item >
    <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
    <el-button @click="resetForm(ruleFormRef)">取消</el-button>
    </el-form-item>
     </el-form>

</template>

<script lang="ts" setup>
import { useStore } from 'vuex'
import{Login} from '../api/api'
import { reactive, ref,} from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
 const store = useStore()
// import store from '../../../../ele/vue-element/src/assets/img/vuepro/vuepro/src/store'
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
 username: 'admin',
 password:'123456',
})

const rules = reactive<FormRules>({
  name: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 10, message: 'Length should be 3 to 10', trigger: 'blur' },
  ],
  password:[
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 10, message: 'Length should be 3 to 1o', trigger: 'blur' },
  ],
  })
  const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const res= await Login(ruleForm)
      console.log(res)
      console.log(res.data.data.token)
       localStorage.setItem('token',res.data.data.token)
     
      store.commit('getToken',res.data.data.token)
      router.push({path:'/'})
     
      // store.commit('sum', num)
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))

function show(){
    console.log('show')
}
 show()
</script>

<style lang="scss" scoped>
.el-form{
    width: 600px;
    height: 400px;
    position: absolute;
    left: 0;
    top: 0;
    transform: translate(50%,50%);
    box-sizing: border-box;
    padding: 20px;
    .el-form-item{
     padding: 20px;

    }
   .el-button{
   width: 80px;
   
    }
}
</style>